/*
 Written by Toadfish.

 Two modules are created. The MessageLocker handles message sending privileges, whereas the MessageCleaner handles cleaning the messages
 for output (that is, remove HTML, cut message length, filter words, et cetera).

 For basic usage, the only procs that matter are MessageCleaner.CleanMessage(message) and MessageLocker.CanSend(client/client, message).
 The latter tells you whether this client is allowed to send a message, while the former is the basic cleaning procedure for messages.
 One other proc, MessageLocker.RegisterMessage(client/client, message), is required to be called when a message is successfully sent.

 This file implements two objects: server_message_cleaner and server_client_locker. These are the server defaults. Additionally,
 it implements message_cleaner hooks for all clients (for example, for a private filter list), in the case future implementations
 allow for such a thing.
 */


 /*

 This is how the say verb should look like to implement this library:

 client/verb/SaySomething(message as text)
 	if (!CanSendMessage(message))
 		return
 	for (var/client/client)
 		var/message_to_send = client.CleanMessage(message)
 		client << ... // (send formatted message_to_send to the client)
 	server_message_locker.RegisterMessage(src, message)

 For example, as of this writing, this is how the Say verb should be modified:

	Say(msg as text)
		set
			hidden = 1
			name = ">"
		if(CanSendMessage(msg))
			if (IsMuted())
				System_UserMessage(src, "You are currently muted.")
			return 0
		else
			if(HasSustenance(msg))
				if(src.Colors)
					for(var/client/C)
						if(!C.IsTelnet()) C << "<b>\[</b>[time2text(world.timeofday,"hh:mm:ss")]<b>\] <font color='#[Name_Color]'>[src.name]</font></b>: <font color='#[Text_Color]'>[C.CleanMessage(msg)]</font>"
						else C <<"<b>\[</b>[time2text(world.timeofday,"hh:mm:ss")]<b>\] [src.name]</b>: [C.CleanMessage(msg)]"
				else
					world<<"<b>\[</b>[time2text(world.timeofday,"hh:mm:ss")]<b>\] [src.name]</b>: [html_encode(msg)]"
			if(src.Status != Available)
				src.Status -= src
				src.Status = Available
				src.Status += src
 */



// Take a look at the code to understand these settings. In future versions the settings can be read from a file, perhaps.
var/MessageCleaner/server_message_cleaner	= new (max_length = 450, filtered_words=list("fuck"="fudge","nigger"="black man"), remove_capitals = FALSE, \
												message_link = TRUE, encode_html = TRUE, filter_whitespace = TRUE)
var/MessageLocker/server_message_locker		= new (interval = 30, per_interval = 5, repeat_max = 3, minimum_length = 1)

MessageLocker/isMuted(client/client)
	if (client) return client.IsMuted()

MessageCleaner/getLongMessageLink(message)
	return "<a href='?longmessage=[message]'>\[...]</a>"

client
	var/MessageCleaner/message_cleaner

	Topic(href, href_list[])
		if (href_list["longmessage"])
			if (!IsTelnet()) src << browse("[href_list["longmessage"]]", "window=Message")
		return ..()

	proc
		/* Cleans a message for the client. */
		CleanMessage(message)
			if (message_cleaner)
				return message_cleaner.CleanMessage(message)
			else return server_message_cleaner.CleanMessage(message)

		/* Returns TRUE if this client can send the provided message. */
		CanSendMessage(message)
			return server_message_locker.CanSend(src, message)

		RegisterMessage(message)
			return server_message_locker.RegisterMessage(src, message)